package aip.assignment2.view.administrator;

import aip.assignment2.persistence.entity.Purchase;
import aip.assignment2.service.Application;
import aip.assignment2.service.PurchaseManager;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 * This managed bean contains all UI data and functions that live through the session scope of the administrator.
 */
@ManagedBean(name = "admin")
@ViewScoped
public class Admin
{
	@EJB
	private Application m_Application;
	@EJB
	private PurchaseManager m_PurchaseManager;
	// <editor-fold desc="Property - currentInputPurchaseNumber - Get/Set" defaultstate="collapsed">
	/**
	 * The current input purchase number
	 */
	private String currentInputPurchaseNumber;
	/**
	 * Gets the current input purchase number
	 */
	public String getCurrentInputPurchaseNumber()
	{
		return currentInputPurchaseNumber;
	}
	/**
	 * Sets the current input purchase number
	 */
	public void setCurrentInputPurchaseNumber(String currentInputPurchaseNumber)
	{
		this.currentInputPurchaseNumber = currentInputPurchaseNumber;
	}
	// </editor-fold>
	// <editor-fold desc="Property - currentPurchases - Get/Set" defaultstate="collapsed">
	/**
	 * The current list of Purchases found.
	 */
	private List<Purchase> currentPurchases;
	/**
	 * Get the current list of Purchases found.
	 */
	public List<Purchase> getCurrentPurchases()
	{
		if (currentPurchases == null)
		{
			currentPurchases = m_PurchaseManager.getOrderedOrPaidPurchases();
		}
		return currentPurchases;
	}
	// </editor-fold>
	// <editor-fold desc="Property - currentPurchase - Get/Set" defaultstate="collapsed">
	/**
	 * the current purchase.
	 */
	private Purchase currentPurchase;
	/**
	 * Get the current purchase.
	 */
	public Purchase getCurrentPurchase()
	{
		return currentPurchase;
	}
	/**
	 * Set the current purchase.
	 */
	public void setCurrentPurchase(Purchase purchase)
	{
		this.currentPurchase = purchase;
	}
	// </editor-fold>
	// <editor-fold desc="Property - statuses - Get" defaultstate="collapsed">
	private List<String> statuses;
	public List<String> getStatuses()
	{
		if (statuses == null)
		{
			statuses = new ArrayList<String>();
			statuses.add("ORDERED");
			statuses.add("PAID");
			statuses.add("SENT");
		}
		return statuses;
	}
	// </editor-fold>
	/**
	 * This method logs out the current user.
	 */
	public String logOut()
	{
		if (m_Application.getCurrentSession() != null)
		{
			m_Application.getCurrentSession().invalidate();
		}
		return "index";
	}
	/**
	 * This method searches the ordered or paid purchases.
	 */
	public void search()
	{
		currentPurchases = m_PurchaseManager.getOrderedOrPaidPurchases(currentInputPurchaseNumber);
	}
	/**
	 * This method searches all ordered or paid purchases.
	 */
	public void searchAll()
	{
		currentPurchases = m_PurchaseManager.getOrderedOrPaidPurchases();
	}
	/**
	 * This method saves all purchases.
	 */
	public void save()
	{
		if (currentPurchases != null)
		{
			for (Purchase purchase : currentPurchases)
			{
				m_PurchaseManager.update(purchase);
			}
		}
		search();
	}
	
	private final int maxCountOfPurchasePage = 5;
	
	public int getMaxCountOfPurchasePerPage()
	{
		return maxCountOfPurchasePage;
	}
}
